//
//  OrdeingViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "OrdeingViewController.h"
#import "orderModel.h"
#import "OrderDetailViewController.h"
#import "RecomOrderViewController.h"
@interface OrdeingViewController ()<UITableViewDataSource , UITableViewDelegate>
@property(nonatomic, assign)BOOL isUpLoading;

@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, strong)NSMutableArray *mineArray;
@property(nonatomic, strong)UIImageView *image;
@end

@implementation OrdeingViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
      self.mineArray = [[FMDBSingleton FMDBDataBase] selectMoreRecommend];
    [self.myTableView reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.mineArray = [[FMDBSingleton FMDBDataBase] selectMoreRecommend];
    self.view.backgroundColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.isUpLoading = NO;
    [self creatTableView];
//    [self creatButton];
}
-(void)creatButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:@"清空订阅" forState:UIControlStateNormal];
    //    [button setImage:[UIImage imageNamed:@"u=1146796800,879196243&fm=21&gp=0.jpg"] forState:UIControlStateNormal];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 15;
    [button addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(5);
        make.left.equalTo(self.view).offset(6);
        make.width.mas_equalTo(160);
        make.bottom.equalTo(self.myTableView.mas_top).offset(-3);
        
    }];
    
    UIButton *buttonDetail = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonDetail.backgroundColor = [UIColor orangeColor];
    [buttonDetail setTitle:@"每日订阅推荐" forState:UIControlStateNormal];
    //    [button setImage:[UIImage imageNamed:@"u=1146796800,879196243&fm=21&gp=0.jpg"] forState:UIControlStateNormal];
    buttonDetail.layer.masksToBounds = YES;
    buttonDetail.layer.cornerRadius = 15;
    [buttonDetail addTarget:self action:@selector(detailAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonDetail];
    [buttonDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(5);
        make.right.equalTo(self.view).offset(-5);
        make.width.mas_equalTo(160);
        make.bottom.equalTo(self.myTableView.mas_top).offset(-6);
        
    }];
    //        UIImageView *image= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u=1280079052,3666822166&fm=21&gp=0.jpg"]];
    //        cell.backgroundView = image;
    //        cell.textLabel.text = @"";
    //        cell.textLabel.textColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
}

-(void)creatTableView
{
    _myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _myTableView.backgroundColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    [self.view addSubview:_myTableView];
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(0);
        make.left.right.bottom.equalTo(self.view);
        
    }];
    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upTableView)];
    [self.myTableView.mj_header beginRefreshing];
    
    
}

- (void)upTableView{
    
    self.mineArray = [[FMDBSingleton FMDBDataBase] selectMoreRecommend];
    if (self.mineArray.count == 0)
    {
        _image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u=2817980817,2240383504&fm=21&gp=0.jpg"]];
        [self.view addSubview:_image];
        [_image mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.myTableView).offset(60);
            make.left.equalTo(self.view).offset(3);
            make.right.equalTo(self.view).offset(-3);
            make.bottom.equalTo(self.view).offset(-200);
        }];
        self.image.hidden = NO;
    }
    else
    {
        self.image.hidden = YES;
    }
    [self.myTableView.mj_header endRefreshing];
    [self.myTableView reloadData];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mineArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInditify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInditify];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    orderModel *model = [self.mineArray objectAtIndex:indexPath.row];
    cell.textLabel.text = model.tname;
    cell.detailTextLabel.text = model.title;
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 4;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.mineArray = [[FMDBSingleton FMDBDataBase] selectMoreRecommend];
    orderModel *model = [self.mineArray objectAtIndex:indexPath.row];
    OrderDetailViewController *detailVC = [[OrderDetailViewController alloc] init];
    detailVC.i = 1;
    detailVC.code = model.ename;
    detailVC.alias = model.alias;
    detailVC.source = model.tname;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideView" object:nil];
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(void)clearAction:(UIButton *)btn
{
    [[FMDBSingleton FMDBDataBase] dropNetEase];
    [self upTableView];
}

-(void)detailAction:(UIButton *)btn
{
    
    RecomOrderViewController *recommend = [[RecomOrderViewController alloc] init];
    [self.navigationController pushViewController:recommend animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
